﻿//using AiMoBridge.Paths;
//using AiMoWindows.CodeScripts;
//using AiMoWindows.ScriptsLibrary;
//using AiMoWindows.Windows;
//using System.IO;
//using System.IO.Compression;
//using System.Text;
//namespace AiMoWindows.CustomizedCommands
//{
//    static class ScriptsLibraryUtils
//    {
//        public static void CreateInitialExamples()
//        {
//            string outputFolder = PathManager.CustomizedCommandsFolder;
//            try
//            {
//                DirectoryInfo outputDirectory = new DirectoryInfo(outputFolder);
//                if (!outputDirectory.Exists) outputDirectory.Create();
//                CodeIO.RepairExtensions(outputFolder);
//                if (outputDirectory.GetFiles($"*{CodeIO.Extension}").Length == 0)
//                {
//                    foreach (LibraryExampleItem item in LibraryExamples.DefaultExamples)
//                    {
//                        string path = Path.Combine(outputFolder, item.Name + CodeIO.Extension);
//                        File.WriteAllText(path, item.Content, Encoding.UTF8);
//                    }
//                }
//            }
//            catch (Exception ex)
//            {
//                DebugLog.Log.Write($"cannot create initial example files \r\n {ex.FormatException()}", "CreateInitialExamplesError");
//#if DEBUG
//                System.Windows.MessageBox.Show($"cannot create initial example files \r\n {ex.FormatException()}");
//#endif
//            }
//        }
//        public static void DebugCreate()
//        {
//            string inputFolderPath = @"D:\Users\wucl12\Desktop\UserCommandsExample";
//            string zipPath = @"D:\Scripts\Aimo\AimoCore\EmbeddedResources\UserCommandsExample.gz";
//            CompressFiles(zipPath, Directory.GetFiles(inputFolderPath));
//        }
//        public static void DebugExtract()
//        {
//            string outputFolder = @"D:\Users\wucl12\Desktop\UserCommandsExample";
//            string zipPath = @"D:\Scripts\Aimo\AimoCore\EmbeddedResources\UserCommandsExample.gz";
//            if (!Directory.Exists(outputFolder)) Directory.CreateDirectory(outputFolder);
//            ExtractFiles(File.ReadAllBytes(zipPath), outputFolder);
//        }
//        static void CompressFiles(string outputPath, string[] inputFiles)
//        {
//            using (MemoryStream memoryStream = new MemoryStream())
//            {
//                foreach (string file in inputFiles)
//                {
//                    MergeFileToStream(memoryStream, file);
//                }
//                byte[] buffer = memoryStream.ToArray();

//                using (FileStream writeStream = new FileStream(outputPath, FileMode.CreateNew))
//                {
//                    using (GZipStream zipStream = new GZipStream(writeStream, CompressionLevel.Optimal))
//                    {
//                        zipStream.Write(buffer, 0, buffer.Length);
//                        zipStream.Close();
//                    }
//                    writeStream.Close();
//                }
//                memoryStream.Close();
//            }
//        }
//        static void MergeFileToStream(Stream streamWriter, string fullPath)
//        {
//            string fileName = Path.GetFileName(fullPath);

//            byte[] fileBytes = File.ReadAllBytes(fullPath);
//            byte[] nameBytes = Encoding.UTF8.GetBytes(fileName);

//            byte[] nameLengthBytes = BitConverter.GetBytes(nameBytes.Length);
//            byte[] fileLengthByte = BitConverter.GetBytes(fileBytes.Length);

//            streamWriter.Write(nameLengthBytes, 0, nameLengthBytes.Length);
//            streamWriter.Write(nameBytes, 0, nameBytes.Length);
//            streamWriter.Write(fileLengthByte, 0, fileLengthByte.Length);
//            streamWriter.Write(fileBytes, 0, fileBytes.Length);
//        }
//        static void ExtractFiles(byte[] buffers, string outputFolder)
//        {
//            using (MemoryStream originalStream = new MemoryStream(buffers))
//            {
//                Console.WriteLine(originalStream.Length);
//                using (MemoryStream outputStream = new MemoryStream())
//                {
//                    using (GZipStream zipStream = new GZipStream(originalStream, CompressionMode.Decompress))
//                    {
//                        zipStream.CopyTo(outputStream);
//                        zipStream.Close();
//                    }
//                    Console.WriteLine(outputStream.Length);
//                    SplitFileFromStream(outputStream, outputFolder);
//                    outputStream.Close();
//                }
//                originalStream.Close();
//            }
//        }
//        static void SplitFileFromStream(Stream stream, string outputFolder)
//        {
//            stream.Position = 0;

//            while (stream.Position < stream.Length)
//            {
//                Console.WriteLine(stream.Position);
//                Console.WriteLine(stream.Length);

//                byte[] nameLengthBytes = new byte[4];
//                stream.Read(nameLengthBytes, 0, 4);
//                int nameLength = BitConverter.ToInt32(nameLengthBytes, 0);

//                byte[] nameBytes = new byte[nameLength];
//                stream.Read(nameBytes, 0, nameLength);
//                string fileName = Encoding.UTF8.GetString(nameBytes);

//                byte[] fileLengthBytes = new byte[4];
//                stream.Read(fileLengthBytes, 0, 4);
//                int fileLength = BitConverter.ToInt32(fileLengthBytes, 0);

//                byte[] fileBytes = new byte[fileLength];
//                stream.Read(fileBytes, 0, fileLength);
//                string path = Path.Combine(outputFolder, fileName);
//                Console.WriteLine(path);
//                File.WriteAllBytes(path, fileBytes);
//            }
//        }

//    }
//}
